
import requests
from bs4 import BeautifulSoup
'''
def FindBook(response):
    soup = BeautifulSoup(response.text,'html.parser')
    #查找书籍信息
    books = soup.find_all('tr',class_='item')
    book_list = list()
    for book in books:
        #查找书籍
        title_tag = book.find('div',class_='pl2').find('a')
        title = title_tag.find('span').text.strip() if title_tag else '未找到书名'
        # 查找评分
        rating_tag = book.find('span', class_='rating_nums')
        rating = rating_tag.text.strip() if rating_tag else '未找到评分'
        book_list.append({
        'title': title,
        'rating': rating
        })
    for book in book_list:
        print(f"书名：{book['title']}，评分：{book['rating']}")
#目标网站:豆瓣读书
url = 'https://book.douban.com/top250'
# 设置请求头，伪装成浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
#发送GET请求，获取网页内容
response = requests.get(url,headers=headers)
#判断请求是否成功
response.encoding = 'utf-8' 
if response.status_code == 200 or response.status_code == 418:
    #print(response.text) #打印网页的HTML内容
    print('请求成功！')
    FindBook(response)

else:
    print('请求失败！')
'''

'''
import requests
from bs4 import BeautifulSoup

# 设置请求头，伪装成浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# 爬取豆瓣读书的 URL
url = 'https://book.douban.com/top250'

# 发送 GET 请求
response = requests.get(url, headers=headers)

# 检查请求是否成功
if response.status_code == 200:
    # 解析 HTML 文档
    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找书籍信息
    books = soup.find_all('tr', class_='item')

    for book in books:
        title = book.find('div', class_='pl2').find('a').find('span').text  # 书名
        rating = book.find('span', class_='rating_nums').text  # 评分
        print(f"书名：{title}，评分：{rating}")

else:
    print(f"请求失败，状态码：{response.status_code}")
'''

'''
import requests
from bs4 import BeautifulSoup

# 设置请求头，伪装成浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# 爬取豆瓣读书的 URL
url = 'https://book.douban.com/top250'

# 发送 GET 请求
response = requests.get(url, headers=headers)

# 检查请求是否成功
if response.status_code == 200:
    # 解析 HTML 文档
    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找书籍信息
    books = soup.find_all('tr', class_='item')

    for book in books:
        # 查找书名
        title_tag = book.find('div', class_='pl2').find('a')
        if title_tag:  # 检查是否找到书名
            title = title_tag.find('span').text.strip()  # 书名
        else:
            title = '未找到书名'

        # 查找评分
        rating_tag = book.find('span', class_='rating_nums')
        if rating_tag:  # 检查是否找到评分
            rating = rating_tag.text.strip()  # 评分
        else:
            rating = '未找到评分'

        print(f"书名：{title}，评分：{rating}")

else:
    print(f"请求失败，状态码：{response.status_code}")

'''



def FindBook(response):
    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找书籍信息
    books = soup.find_all('tr', class_='item')

    for book in books:
        # 查找书名
        title_tag = book.find('div', class_='pl2').find('a')  # 查找书名标签
        
        if title_tag and title_tag.find('span'):  # 确保 title_tag 和其子元素都存在
            title = title_tag.find('span').text.strip()  # 书名
        else:
            title = '未找到书名'  # 如果 title_tag 或子元素不存在

        # 查找评分
        rating_tag = book.find('span', class_='rating_nums')
        if rating_tag:  # 检查评分标签是否存在
            rating = rating_tag.text.strip()  # 评分
        else:
            rating = '未找到评分'  # 如果评分标签不存在

        print(f"书名：{title}，评分：{rating}")

# 示例：发送请求并调用函数
url = 'https://book.douban.com/top250'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)

if response.status_code == 200:
    FindBook(response)
else:
    print(f"请求失败，状态码：{response.status_code}")